package com.gz.thread.syc;

public class Bank {
    public static void main(String[] args) {
        Account account = new Account(100);

        new Banks(50,account).start();
        new Banks(50,account).start();
        new Banks(150,account).start();
//        new Thread(new Banks(50,account),"a").start();
//        new Thread(new Banks(150,account),"b").start();
    }
}


class Account {
    public int money;

    public Account(int money) {
        this.money = money;
    }
}

class Banks extends Thread {

    public int money;
    public Account account;

    public  void run() {
        synchronized(account){
            if (this.account.money < money) {
                System.out.println(Thread.currentThread().getName() + "---余额不足");
                return;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.account.money = this.account.money - money;
            System.out.println(Thread.currentThread().getName() + "---取了" + money + "。 剩余：" + this.account.money);
        }
    }

    Banks(int money, Account account) {
        this.money = money;
        this.account = account;
    }
}